import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-12-23
 * Time:11:16
 */
@WebServlet("/hello")
public class Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("hello");
//        resp.getWriter().write("hello");
        //确定文件内容
        //resp.setContentType("text/html");
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("\n");
        //协议名称和版本号
        stringBuilder.append(req.getProtocol());
        stringBuilder.append("\n");
        //获取方法
        stringBuilder.append(req.getMethod());
        stringBuilder.append("\n");

        //获取地址
        stringBuilder.append(req.getRequestURI());
        stringBuilder.append("\n");

        stringBuilder.append(req.getContextPath());
        stringBuilder.append("\n");

        //返回url中的查询字符
        stringBuilder.append(req.getQueryString());
        stringBuilder.append("\n");

        //header中的数据
        Enumeration<String> hearNames= req.getHeaderNames();
        while(hearNames.hasMoreElements()){
            String name= hearNames.nextElement();
            String value=req.getHeader(name);
            stringBuilder.append(name+":"+value);
            stringBuilder.append("\n");
        }
        resp.getWriter().write(stringBuilder.toString());

    }
}
